package com.example.jingwei.tian.nativeh5test;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JavaCallJSActivity extends AppCompatActivity {

    @BindView(R.id.btn1)
    Button mBtn1;
    @BindView(R.id.btn2)
    Button mBtn2;
    @BindView(R.id.wv_java_call_js)
    WebView mWvJavaCallJs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_call_js);
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        //设置WebView支持JS
        mWvJavaCallJs.getSettings().setJavaScriptEnabled(true);
        //从assets目录下加载html
        mWvJavaCallJs.loadUrl("file:///android_asset/web.html");
        //自定义WebView的背景颜色,设置为透明色
        mWvJavaCallJs.setBackgroundColor(Color.TRANSPARENT);
        //设置背景图片
        //mWvJavaCallJs.setBackgroundResource(R.drawable.bgcolorblue);
        MWebViewClient mWebViewClient = new MWebViewClient(mWvJavaCallJs,this);
        mWvJavaCallJs.setWebViewClient(mWebViewClient);
        MWebChromeClient mWebChromeClient = new MWebChromeClient(this);
        mWvJavaCallJs.setWebChromeClient(mWebChromeClient);
        //添加js调用android(java)的方法接口
        MJavascriptInterface mJavascriptInterface = new MJavascriptInterface(this);
        mWvJavaCallJs.addJavascriptInterface(mJavascriptInterface,"web");
        mWvJavaCallJs.addJavascriptInterface(mJavascriptInterface,"jump");


    }


    @OnClick({R.id.btn1, R.id.btn2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                mWvJavaCallJs.loadUrl("javascript:javacalljs()"); // 无参数调用
                break;
            case R.id.btn2:
                mWvJavaCallJs.loadUrl("javascript:javacalljsparam(" + "'123'" + ")"); // 有参数调用
                break;
        }
    }
}